package com.dk.service.impl;

import com.dk.mapper.MenuMapper;
import com.dk.mapper.MenuRoleMapper;
import com.dk.pojo.Menu;
import com.dk.pojo.User;
import com.dk.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    MenuRoleMapper menuRoleMapper;

    @Override
    public List<Map<String, Object>> getMenu(User user) {
        List<Menu> menus = menuMapper.getMenuByUserId(user);
        List<Map<String, Object>> menuTree = getMenuTree(menus, 0);
        return menuTree;

    }

    @Override
    public List<Map<String, Object>> getzTree() {
        List<Menu> menus = menuMapper.selectByExample(null);
        List<Map<String, Object>> menuzTree = getMenuzTree(menus, 0);
        return menuzTree;
    }

    @Override
    public int addMenuInfo(Menu menu) {
        return menuMapper.insertSelective(menu);
    }

    @Override
    public Menu getMenuById(Integer nodeId) {
        return menuMapper.selectByPrimaryKey(nodeId);
    }

    @Override
    public int updateMenuById(Menu menu) {
        return menuMapper.updateByPrimaryKeySelective(menu);
    }

    @Override
    public int deleteMenuById(Integer nodeId) {
        return menuMapper.deleteByPrimaryKey(nodeId);
    }

    @Override
    public List<Map<String, Object>> getzTreeByRid(Integer rid) {
        List<Integer> list = menuRoleMapper.getMidsByRid(rid);
        List<Menu> menus = menuMapper.selectByExample(null);
        List<Map<String, Object>> mapList = getzTreeByRid(menus, 0, list);
        return mapList;
    }

    public List<Map<String, Object>> getzTreeByRid(List<Menu> list,Integer parentId,List<Integer> mids){
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = list.get(i);
            if (parentId.equals(menu.getParentId())){
                map = new HashMap<>();
                map.put("id",menu.getMid());
                map.put("name",menu.getMenuName());
                if (mids.contains(menu.getMid())){
                    map.put("checked",true);
                }
                //当前对象的id当做父级id找到子节点进行拼接
                map.put("children",getzTreeByRid(list,menu.getMid(),mids));
                // 0 1
            }
            if (map != null) {
                List<Map<String, Object>> childrenList = (List<Map<String, Object>>) map.get("children");
                if (childrenList.size() <= 0) {
                    map.remove("children");
                }
                mapList.add(map);
            }
        }
        return mapList;
    }


    public List<Map<String, Object>> getMenuzTree(List<Menu> list,Integer parentId){
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = list.get(i);
            if (parentId.equals(menu.getParentId())){
                map = new HashMap<>();
                map.put("id",menu.getMid());
                map.put("name",menu.getMenuName());
                //当前对象的id当做父级id找到子节点进行拼接
                map.put("children",getMenuzTree(list,menu.getMid()));
                // 0 1
            }
            if (map != null) {
                List<Map<String, Object>> childrenList = (List<Map<String, Object>>) map.get("children");
                if (childrenList.size() <= 0) {
                    map.remove("children");
                }
                mapList.add(map);
            }
        }
        return mapList;
    }

    public List<Map<String, Object>> getMenuTree(List<Menu> list,Integer parentId){
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = list.get(i);
            if (parentId.equals(menu.getParentId())){
                map = new HashMap<>();
                map.put("id",menu.getMid());
                map.put("url",menu.getUrl());
                map.put("text",menu.getMenuName());
                map.put("icon",menu.getIcon());
                //当前对象的id当做父级id找到子节点进行拼接
                map.put("nodes",getMenuTree(list,menu.getMid()));
                // 0 1
            }
            if (map!=null){
                List<Map<String, Object>> nodes = (List<Map<String, Object>>) map.get("nodes");
                if (nodes.size() <= 0) {
                    map.remove("nodes");
                }
                mapList.add(map);
            }
        }
        return mapList;
    }

}
